package com.ling.plasticene.recyclerview.loadmore;

import android.annotation.TargetApi;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;


import com.ling.plasticene.R;
import com.ling.plasticene.common.Stateable;

import java.util.HashMap;
import java.util.Map;


/**
 * Function：默认Load More view
 *
 * Created by ling(quan.ling@hotmail.com) on 15/11/2.
 */
public class DefaultLoadMoreView extends RelativeLayout implements LoadMoreAdapter.LoadMoreUIHandler {
    protected TextView textView;
    protected ProgressBar progressBar;

    Map<Integer, String> mExtendStates;

    public DefaultLoadMoreView(Context context) {
        super(context);
        init();
    }

    public DefaultLoadMoreView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public DefaultLoadMoreView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    @TargetApi(21)
    public DefaultLoadMoreView(Context context, AttributeSet attrs, int defStyleAttr,
                               int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init();
    }

    private void init() {
        LayoutInflater.from(getContext()).inflate(R.layout.layout_default_load_more, this);

        textView = (TextView)findViewById(R.id.textView);
        progressBar = (ProgressBar)findViewById(R.id.progressBar);

        mExtendStates = new HashMap<>();
    }

    @Override
    public View getView() {
        return this;
    }

    @Override
    public void setState(final LoadMoreAdapter loadMoreAdapter, int state) {
        switch (state) {
            case Stateable.NORMAL:
                textView.setText(R.string.loadmore_state_normal);
                textView.setOnClickListener(null);
                progressBar.setVisibility(GONE);
                break;
            case Stateable.LOADING:
                textView.setText(R.string.loadmore_state_loading);
                textView.setOnClickListener(null);
                progressBar.setVisibility(VISIBLE);
                break;
            case Stateable.EMPTY:
                textView.setText(R.string.loadmore_state_no_more);
                textView.setOnClickListener(null);
                progressBar.setVisibility(GONE);
                break;
            case Stateable.ERROR:
                textView.setText(R.string.loadmore_state_fail);
                textView.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        loadMoreAdapter.setState(Stateable.LOADING);
                    }
                });
                progressBar.setVisibility(GONE);
                break;
            default:
                progressBar.setVisibility(GONE);
                if (mExtendStates.containsKey(state)) {
                    textView.setText(mExtendStates.get(state));
                }
        }
    }

    /**
     * 添加简单的自定义状态，并添加对应文字
     *
     * @param state
     * @param text
     */
    public void addState(int state, String text) {
        addState(state, text, null);
    }

    /**
     *
     * @param state
     * @param text
     * @param onClickListener
     */
    public void addState(int state, String text, OnClickListener onClickListener) {
        mExtendStates.put(state, text);
    }
}
